package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysUserSvc;
import com.kmxd.ams.client.system.dto.*;
import com.kmxd.ams.client.system.vo.AccountInfoVO;
import com.kmxd.ams.client.system.vo.SysUserListVO;
import com.kmxd.ams.client.system.vo.SysUserVO;
import com.kmxd.ams.core.common.bo.LoginUser;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 前端控制器
 *
 * @author admin
 * @since 2021-06-10
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/system/sysUser")
@AllArgsConstructor
public class SysUserCtl {

  private ISysUserSvc sysUserSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysUserListVO>> selectPage(Page reqPage, SysUserQueryDTO req) {
    return R.success(sysUserSvc.selectPage(reqPage, req));
  }

  /** 查询用户 */
  @GetMapping("/selectList")
  @AutoLog(module = "查询用户", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询用户")
  public R<List<SysUserListVO>> selectList(SysUserQueryDTO req) {
    req.setDeptId(
        ObjectUtil.isNotEmpty(req.getDeptId())
            ? req.getDeptId()
            : SecurityUtil.getLoginUser().getDeptId());
    return R.success(sysUserSvc.selectList(req));
  }

  /** 根据单位查询用户 */
  @GetMapping("/selectListAll")
  @AutoLog(
      module = "查询用户",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询用户不限制条件")
  public R<List<SysUserListVO>> selectListAll(SysUserQueryDTO req) {
    return R.success(sysUserSvc.selectListAll(req));
  }

  /** 根据角色类型或部门ID查询 */
  @GetMapping("/selectListByRoleOrDept")
  @AutoLog(module = "查询用户", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询用户")
  public R<List<SysUserListVO>> selectListByRoleOrDept(SysUserQueryDTO req) {
    return R.success(sysUserSvc.selectListByRoleOrDept(req));
  }

  /** 根据角色id查询用户 */
  @GetMapping("/getUserListByRoleId")
  @Operation(summary = "根据角色id查询用户")
  public R<Page<SysUserListVO>> getUserListByRoleId(Page reqPage, Long roleId) {
    return R.success(sysUserSvc.getUserListByRoleId(reqPage, roleId));
  }

  /** 根据全宗查询用户 */
  @GetMapping("/getUserListByFondsId")
  @Operation(summary = "根据全宗查询用户")
  public R<Page<SysUserListVO>> getUserListByFondsId(Page reqPage, Long fondsId) {
    return R.success(sysUserSvc.getUserListByFondsId(reqPage, fondsId));
  }

  /** 根据角色类型，查询当前单位下用户 */
  @GetMapping("/selectListByRoleType")
  @Operation(summary = "根据角色类型，查询当前单位下用户")
  public R<List<SysUserListVO>> selectListByRoleType(SysUserRoleTypeQueryDTO req) {
    return R.success(sysUserSvc.selectListByRoleType(req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysUser:add")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysUserAddDTO req) {
    sysUserSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysUser:edit")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysUserEditDTO req) {
    sysUserSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysUser:remove")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysUserSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysUserVO> view(@RequestParam Long id) {
    return R.success(sysUserSvc.view(id));
  }

  @GetMapping("/export")
  @SaCheckPermission("system:sysUser:export")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(SysUserQueryDTO req, HttpServletResponse response) {
    String filename = "用户列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(SysUserListVO.class)
        .sheet("用户列表")
        .doWrite(sysUserSvc.selectList(req));
  }

  /** 重置密码 */
  @PostMapping("/resetPwd")
  @SaCheckPermission("system:sysUser:resetPwd")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.MEDIUM, title = "重置密码")
  public R<Void> resetPwd(@Validated @RequestBody SysUserResetPwdDTO req) {
    sysUserSvc.resetPwd(req);
    return R.success();
  }

  /** 修改密码 */
  @PostMapping("/updatePwd")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.MEDIUM, title = "修改密码")
  public R<Void> updatePwd(@Validated @RequestBody UpdatePwdDTO req) {
    sysUserSvc.updatePwd(req);
    return R.success();
  }

  @GetMapping("/getUserInfo")
  @AutoLog(
      module = "用户管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.MEDIUM,
      title = "获取用户信息")
  public R<LoginUser> getUserInfo() {
    return R.success(sysUserSvc.getUserInfo());
  }

  @GetMapping("/getPermCode")
  @AutoLog(
      module = "用户管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.MEDIUM,
      title = "获取权限集合")
  public R<Set<String>> getPermCode() {
    return R.success(sysUserSvc.getPermCode());
  }

  @GetMapping("/getAccountInfo")
  @AutoLog(module = "用户管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "获取账户信息")
  public R<AccountInfoVO> getAccountInfo() {
    return R.success(sysUserSvc.getAccountInfo());
  }

  @PostMapping("/saveAccountInfo")
  @AutoLog(
      module = "用户管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.MEDIUM,
      title = "保存用户信息")
  public R<Void> saveAccountInfo(@Validated @RequestBody AccountInfoVO req) {
    sysUserSvc.saveAccountInfo(req);
    return R.success();
  }

  @GetMapping("/getCardNo")
  @AutoLog(
          module = "用户管理",
          type = LogTypeEnum.SYSTEM,
          level = LogLevelEnum.MEDIUM,
          title = "获取用户卡编号")
  public R<SysUserVO> getCardNo(@RequestParam Long id) {
    return R.success(sysUserSvc.getCardNo(id));
  }
}
